﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SumOfDigits
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please, input number:");
            Console.WriteLine("Sum of digits ={0}", SumOfDigits(ReadIntFromConsole()));
            Console.WriteLine("Please, input number:");
            Console.WriteLine("Sum of digits ={0}", SumOfDigitsStr(Console.ReadLine()));
        }

        static int SumOfDigits(int number)
        {
            number = Math.Abs(number);
            int sum = 0;
            while (number != 0)
            {
                sum += number % 10;
                number /= 10;
            }

            return sum;
        }

        static int SumOfDigitsStr(string str)
        {
            int sum = 0;
            int num;
            for (int i = 0; i <= str.Length; i++)
            {
                try
                {
                    num = Convert.ToInt32(str[i]) - 48;
                    if (0 <= num & num <= 9)
                    {
                        sum += num;
                    }
                }
                catch
                {
                    sum += 0; 
                }
            }
            return sum;
        }

        static int ReadIntFromConsole()
        {
            int number = 0;
            bool flag = false;
            while (!flag)
            {
                try
                {
                    number = Int32.Parse(Console.ReadLine());
                    flag = true;
                }
                catch
                {
                    flag = false;
                }
            }
            return number;

        }
    }
}
